import axios from "axios"

// 请求拦截器
axios.interceptors.request.use(
    function (config) {
        // 检查是否是需要排除的请求，比如登录请求
        if (config.url.endsWith('/login')) {
            return config;
        }
        // 在请求被发送之前做些什么
        console.log("config###", config);
        let token = localStorage.getItem('token')
        if (!token) {
            console.log("没有token");
            location.href = '/login'
            return null
        }
        config.headers.token = token; // 添加请求头
        // config.headers.common['token'] ='123lx'
        // console.log("common###",config.headers.common);
        console.log("我被拦截了");
        return config;
    },
    function (error) {
        // 对请求错误做些什么
        return Promise.reject(error);
    }
);


const createAxiosInstance = () => {
    const token = localStorage.getItem('token')
    if (!token) {
        return
    }
    axios.create({
        // baseURL: 'https://your-api-base-url.com',
        timeout: 10000,
        headers: {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer YourAccessTokenHere'
        }
    })
};

export const instance = createAxiosInstance();